<!-- 增加文章对话框 -->
<template>
  <div class="container">
    <el-dialog :title="`${data.id ? '修改': '新增'}文章`" :visible.sync="dialogVisible" width="50%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="ruleForm.title" placeholder="请输入文章标签"></el-input>
        </el-form-item>
        <el-form-item label="文章内容" prop="articleBody">
          <quill-editor v-model="ruleForm.articleBody" :options="editorOptions"></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址" prop="videoURL">
          <el-input v-model="ruleForm.videoURL" placeholder="请输入视频地址"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirm()">确认</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { add, update } from '@/api/hmmm/articles'
export default {
  components: {
    quillEditor
  },
  props: {
    data: {
      default: () => {},
      type: Object
    }
  },
  data () {
    return {
      dialogVisible: false,
      ruleForm: {
        id: null,
        title: null,
        articleBody: null,
        videoURL: null
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', tirgger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', tirgger: 'blur' }
        ]
      },
      editorOptions: {
        placeholder: '',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            [{ list: 'ordered' }, { list: 'bullet' }],
            ['blockquote'],
            ['code-block', 'image', 'link']
          ]
        }
      }
    }
  },
  methods: {
    showAddArticle () {
      this.dialogVisible = true
      if (this.data.id) {
        const { id, title, articleBody, videoURL } = this.data
        this.ruleForm = { id, title, articleBody, videoURL }
      } else {
        this.ruleForm = {
          id: null,
          title: null,
          articleBody: null,
          videoURL: null
        }
      }
      this.$nextTick(() => {
        this.$refs.ruleForm.clearValidate()
      })
    },
    async confirm () {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          if (this.data.id) {
            this.ruleForm.id = this.data.id
            await update(this.ruleForm)
          } else {
            await add(this.ruleForm)
          }
          this.$message.success('添加成功')
          this.dialogVisible = false
          this.$emit('changeArticleList')
        }
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.container ::v-deep .ql-editor {
  height: 200px;
}
</style>
